import math
from collections import deque
import typing


class Header:
    def __init__(self, no: str, rev: str, title: str):
        self.no = no
        self.rev = rev
        self.title = title

    __slots__ = ["no", "rev", "title"]


class DrawingRecord:
    def __init__(self, seq: str, no: str, sheet: str, eo1: str, eo2: str = '', eo3: str = '', eo4: str = '',
                 eo5: str = '', eo6: str = ''):
        self.seq = seq
        self.no = no
        self.sheet = sheet
        self.eo1 = eo1
        self.eo2 = eo2
        self.eo3 = eo3
        self.eo4 = eo4
        self.eo5 = eo5
        self.eo6 = eo6

    __slots__ = ["seq", "no", "sheet", "eo1", "eo2", "eo3", "eo4", "eo5", "eo6"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


class Model:
    def __init__(self):
        self.model = ''
        self.partNumber = ''
        self.proNumber = ''
        self.interchange = ''
        self.compiled = ''
        self.checked = ''
        self.qa = ''
        self.appr = ''
        self.ki = ''
        self._drawings: typing.Deque[DrawingRecord] = deque()
        self._pageSize = 9

    __slots__ = ["model", "partNumber", "proNumber", "interchange", "compiled", "checked", "qa", "appr", "ki",
                 "_drawings",
                 "_pageSize"]

    def addDrawingRecord(self, record: DrawingRecord):
        self._drawings.append(record)

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._drawings, self._pageSize)

    def getForOnePage(self) -> typing.List[DrawingRecord]:
        i = 1
        rs = []
        while self._drawings and i <= self._pageSize:
            rs.append(self._drawings.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[DrawingRecord]:
        rs = []
        while self._drawings:
            rs.append(self._drawings.popleft())

        return rs


class RevisionRecord:

    def __init__(self, rev: str = '', date: str = '', eff: str = '', change: str = '', source: str = '',
                 revised: str = '', approved: str = '',
                 buyer: str = ''):
        self.rev = rev
        self.date = date
        self.eff = eff
        self.change = change
        self.source = source
        self.revised = revised
        self.approved = approved
        self.buyer = buyer

    __slots__ = ["rev", "date", "eff", "change", "source", "revised", "approved", "buyer"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


# 更改记录
class Revision:
    def __init__(self):
        self._dataList: typing.Deque[RevisionRecord] = deque()
        self._pageSize = 6

    __slots__ = ["_dataList", "_pageSize"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._dataList, self._pageSize)

    def addRevisionRecord(self, record: RevisionRecord):
        self._dataList.append(record)

    def getForOnePage(self) -> typing.List[RevisionRecord]:
        i = 1
        rs = []
        while self._dataList and i <= self._pageSize:
            rs.append(self._dataList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[RevisionRecord]:
        rs = []
        while self._dataList:
            rs.append(self._dataList.popleft())
        return rs


class DocumentRecord:
    def __init__(self, no: str, name: str):
        self.seq = ''
        self.no = no
        self.name = name

    __slots__ = ["seq", "no", "name"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)

    def setSeq(self, seq: str):
        self.seq = seq


# 所需工艺规范和一般资料
class TDocument:
    def __init__(self):
        self._processList: typing.Deque[DocumentRecord] = deque()
        self._processSeq = 0
        self._processPageSize = 13
        self._generalList: typing.Deque[DocumentRecord] = deque()
        self._generalSeq = 0
        self._generalPageSize = 4

    __slots__ = ["_processList", "_processSeq", "_processPageSize", "_generalList", "_generalSeq", "_generalPageSize"]

    def getPageCountNeeded(self):
        processNeeded = calcPageCountNeeded(self._processList, self._processPageSize)
        generalNeeded = calcPageCountNeeded(self._generalList, self._generalPageSize)
        return max(processNeeded, generalNeeded)

    def addProcess(self, record: DocumentRecord):
        self._processSeq = self._processSeq + 1
        record.setSeq(f'{self._processSeq}')
        self._processList.append(record)

    def addGeneral(self, record: DocumentRecord):
        self._generalSeq = self._generalSeq + 1
        record.setSeq(f'{self._generalSeq}')
        self._generalList.append(record)

    def getProcessesForOnePage(self) -> typing.List[DocumentRecord]:
        i = 1
        rs = []
        while self._processList and i <= self._processPageSize:
            rs.append(self._processList.popleft())
            i += 1
        return rs

    def getGeneralsForOnePage(self) -> typing.List[DocumentRecord]:
        i = 1
        rs = []
        while self._generalList and i <= self._generalPageSize:
            rs.append(self._generalList.popleft())
            i += 1
        return rs

    def getProcessesAll(self) -> typing.List[DocumentRecord]:
        rs = []
        while self._processList:
            rs.append(self._processList.popleft())
        return rs

    def getGeneralsAll(self) -> typing.List[DocumentRecord]:
        rs = []
        while self._generalList:
            rs.append(self._generalList.popleft())
        return rs


class ResourceRecord:
    def __init__(self, seq: str, no: str, name: str, spec1: str, spec2: str, spec3: str, spec4: str):
        self.seq = seq
        self.no = no
        self.name = name
        self.spec1 = spec1
        self.spec2 = spec2
        self.spec3 = spec3
        self.spec4 = spec4

    __slots__ = ["seq", "no", "name", "spec1", "spec2", "spec3", "spec4"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


# 资源表
class Resources:
    def __init__(self):
        self._resourceList: typing.Deque[ResourceRecord] = deque()
        self._resourcePageSize = 17

    __slots__ = ["_resourceList", "_resourcePageSize"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._resourceList, self._resourcePageSize)

    def addResourceRecord(self, record: ResourceRecord):
        self._resourceList.append(record)

    def getForOnePage(self) -> typing.List[ResourceRecord]:
        i = 1
        rs = []
        while self._resourceList and i <= self._resourcePageSize:
            rs.append(self._resourceList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[ResourceRecord]:
        rs = []
        while self._resourceList:
            rs.append(self._resourceList.popleft())
        return rs


class PartRecord:
    def __init__(self, seq: str, no: str, name: str, spec: str, typ: str, qty: str, nex: str, note: str):
        self.seq = seq
        self.no = no
        self.name = name
        self.spec = spec
        self.typ = typ
        self.qty = qty
        self.nex = nex
        self.note = note

    __slots__ = ["seq", "no", "name", "spec", "typ", "qty", "nex", "note"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


# 零件表
class TPart:
    def __init__(self):
        self._partList: typing.Deque[PartRecord] = deque()
        self._partPageSize = 17

    __slots__ = ["_partList", "_partPageSize"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._partList, self._partPageSize)

    def addPart(self, part: PartRecord):
        self._partList.append(part)

    def getForOnePage(self) -> typing.List[PartRecord]:
        i = 1
        rs = []
        while self._partList and i <= self._partPageSize:
            rs.append(self._partList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[PartRecord]:
        rs = []
        while self._partList:
            rs.append(self._partList.popleft())
        return rs


# 草图表
class Sketch:
    def __init__(self):
        self._imagePathList: typing.Deque[str] = deque()
        self._pageSize = 1

    __slots__ = ["_imagePathList", "_pageSize"]

    def getPageCountNeeded(self):
        if len(self._imagePathList) == 0:
            return 1
        else:
            return len(self._imagePathList)

    def addImage(self, imagePath: str):
        self._imagePathList.append(imagePath)

    def getForOnePage(self) -> typing.List[str]:
        i = 1
        rs = []
        while self._imagePathList and i <= self._pageSize:
            rs.append(self._imagePathList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[str]:
        rs = []
        while self._imagePathList:
            rs.append(self._imagePathList.popleft())
        return rs


class ProcessRecord:
    def __init__(self, seq: str, process: str, instructions: str, tooling: str, eg: str, key: str):
        self.seq = seq
        self.process = process
        self.instructions = instructions
        self.tooling = tooling
        self.eg = eg
        self.key = key

    __slots__ = ['seq', 'process', 'instructions', 'tooling', 'eg', 'key']

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


# 工序表
class Process:
    def __init__(self):
        self._processList: typing.Deque[ProcessRecord] = deque()
        self._processPageSize = 5

    __slots__ = ['_processList', '_processPageSize']

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._processList, self._processPageSize)

    def addProcess(self, processRecord: ProcessRecord):
        self._processList.append(processRecord)

    def getForOnePage(self) -> typing.List[ProcessRecord]:
        i = 1
        rs = []
        while self._processList and i <= self._processPageSize:
            rs.append(self._processList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[ProcessRecord]:
        rs = []
        while self._processList:
            rs.append(self._processList.popleft())
        return rs


class ARModel:
    def __init__(self):
        self.model = ''
        self.programName = ''
        self.ki = ''

    __slots__ = ['model', 'programName', 'ki']

    #     self.__split = deque()
    #     self.__pageSize = 6
    #
    # def getPageCountNeeded(self):
    #     return calcPageCountNeeded(self.__split, self.__pageSize)

    # def getForOnePage(self) -> typing.List[DrawingRecord]:
    #     i = 1
    #     rs = []
    #     while self.__split and i <= self.__pageSize:
    #         rs.append(self.__split.popleft())
    #         i += 1
    #     return rs
    #
    # def getAll(self) -> typing.List[DrawingRecord]:
    #     rs = []
    #     while self.__split:
    #         rs.append(self.__split.popleft())
    #
    #     return rs


class ARSeqRecord:
    def __init__(self, seq: str = '', process: str = '', record: str = '', op: str = '', qty: str = '',
                 inspec: str = '', nQty: str = '', nNo: str = '',
                 note: str = '', key: str = ''):
        self.seq = seq
        self.process = process
        self.record = record
        self.op = op
        self.qty = qty
        self.inspec = inspec
        self.nQty = nQty
        self.nNo = nNo
        self.note = note
        self.key = key

    __slots__ = ["seq", "process", "record", "op", "qty", "inspec", "nQty", "nNo", "note", "key"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


class ARSeq:
    def __init__(self):
        self._dataList: typing.Deque[ARSeqRecord] = deque()
        self._pageSize = 7

    __slots__ = ["_dataList", "_pageSize"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._dataList, self._pageSize)

    def addRecord(self, record: ARSeqRecord):
        self._dataList.append(record)

    def getForOnePage(self) -> typing.List[ARSeqRecord]:
        i = 1
        rs = []
        while self._dataList and i <= self._pageSize:
            rs.append(self._dataList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[ARSeqRecord]:
        rs = []
        while self._dataList:
            rs.append(self._dataList.popleft())
        return rs


class ARPartRecord:
    def __init__(self, seq: str = '', no: str = '', name: str = '', rev: str = '', typ: str = '', qty: str = '',
                 traNo: str = '', note: str = ''):
        self.seq = seq
        self.no = no
        self.name = name
        self.rev = rev
        self.typ = typ
        self.qty = qty
        self.traNo = traNo
        self.note = note

    __slots__ = ["seq", "no", "name", "rev", "typ", "qty", "traNo", "note"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


class ARPart:
    def __init__(self):
        self._dataList: typing.Deque[ARPartRecord] = deque()
        self._pageSize = 19

    __slots__ = ["_dataList", "_pageSize"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._dataList, self._pageSize)

    def addRecord(self, record: ARPartRecord):
        self._dataList.append(record)

    def getForOnePage(self) -> typing.List[ARPartRecord]:
        i = 1
        rs = []
        while self._dataList and i <= self._pageSize:
            rs.append(self._dataList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[ARPartRecord]:
        rs = []
        while self._dataList:
            rs.append(self._dataList.popleft())
        return rs


class ARRefRecord:
    def __init__(self, seq: str = '', no: str = '', name: str = '', iss: str = '', final: str = '', ana: str = ''):
        self.seq = seq
        self.no = no
        self.name = name
        self.iss = iss
        self.final = final
        self.ana = ana

    __slots__ = ["seq", "no", "name", "iss", "final", "ana"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


class ARRef:
    def __init__(self):
        self._dataList: typing.Deque[ARRefRecord] = deque()
        self._pageSize: int = 13
        self.sign: str = ''
        self.signDate: str = ''

    __slots__ = ["_dataList", "_pageSize", "sign", "signDate"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._dataList, self._pageSize)

    def addRecord(self, record: ARRefRecord):
        self._dataList.append(record)

    def getForOnePage(self) -> typing.List[ARRefRecord]:
        i = 1
        rs = []
        while self._dataList and i <= self._pageSize:
            rs.append(self._dataList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[ARRefRecord]:
        rs = []
        while self._dataList:
            rs.append(self._dataList.popleft())
        return rs


class AREngChangeRecord:
    def __init__(self, seq: str = '', no: str = '', ver: str = '', name: str = '', quant: str = '', changeNo: str = '',
                 procNo: str = '', procVer: str = ''):
        self.seq = seq
        self.no = no
        self.ver = ver
        self.name = name
        self.quant = quant
        self.changeNo = changeNo
        self.procNo = procNo
        self.procVer = procVer

    __slots__ = ["seq", "no", "ver", "name", "quant", "changeNo", "procNo", "procVer"]

    def __getitem__(self, index):
        attr = self.__slots__[index]
        return getattr(self, attr)


class AREngChange:
    def __init__(self):
        self._dataList: typing.Deque[AREngChangeRecord] = deque()
        self._pageSize = 15

    __slots__ = ["_dataList", "_pageSize"]

    def getPageCountNeeded(self):
        return calcPageCountNeeded(self._dataList, self._pageSize)

    def addRecord(self, record: AREngChangeRecord):
        self._dataList.append(record)

    def getForOnePage(self) -> typing.List[AREngChangeRecord]:
        i = 1
        rs = []
        while self._dataList and i <= self._pageSize:
            rs.append(self._dataList.popleft())
            i += 1
        return rs

    def getAll(self) -> typing.List[AREngChangeRecord]:
        rs = []
        while self._dataList:
            rs.append(self._dataList.popleft())
        return rs


def calcPageCountNeeded(dataList: deque, pageSize: int):
    if len(dataList) == 0:
        return 1
    else:
        return math.ceil(len(dataList) / pageSize)
